class CreateAttachments < ActiveRecord::Migration
  def self.up
    create_table :attachments do |t|
      t.column :name, :string;
      t.column :mime_type, :string;
      t.column :public, :boolean, :default => 1;
      t.column :storable_id, :integer;
      t.column :storable_type, :string;
      t.column :created_at, :datetime;
    end
    say_with_time "Creating attachments folder..." do
      Dir.chdir "public";
      Dir.mkdir "attachments" unless File.directory? "attachments";
      Dir.chdir "../";
    end
  end

  def self.down
    require "ftools";
    say_with_time "Removing attachments folder..." do
      Attachment.destroy_all;
      begin
        Dir.rmdir "#{RAILS_ROOT}/public/attachments";
      rescue
        say "Can't remove attachments directory: #{$!}"
      end
    end
    drop_table :attachments
  end
end
